(function ($) {
    $.fn.Slider = function (options) {
        "use strict";

        var settings = $.extend({
            isAuto: true,
            transTime: 4000,
            animateSpeed: 1000,
            sliderMode: 'slide', //'slide | fade',
            pointerControl: true,
            pointerEvent: 'click',//'hover' | 'click',
            arrowControl: true,
        }, options);
        var interval;
        var isAnimating = false;
        var $slider = $(this);
        var $sliderWrap = $slider.find('.slider-inner');
        var sliderCount = $sliderWrap.find('> .item').length;
        var sliderWidth = $slider.width();
        var currentIndex = 0;

        var sliderFun = {
            controlInit: function () {
                // pointerControl
                if (settings.pointerControl) {

                    var html = '';
                    html += '<ol class="slider-pointer">';
                    for (var i = 0; i < sliderCount; i++) {
                        if (i == 0) {
                            html += '<li class="active"></li>'
                        } else {
                            html += '<li></li>'
                        }
                    }
                    html += '</ol>'
                    $slider.append(html);
                    // 指示器居中
                    var $pointer = $slider.find('.slider-pointer');
                    $pointer.css({
                        left: '50%',
                        marginLeft: -$pointer.width() / 2
                    });
                }
                // pointerControl
                if (settings.arrowControl) {
                    var html = "";
                    html += '<span class="slider-control prev">&lt;</span>';
                    html += '<span class="slider-control next">&gt;</span>'
                    $slider.append(html);
                }
                $slider.on('click', '.slider-control.prev', function (event) {
                    sliderFun.toggleSlide('prev');
                });
                $slider.on('click', '.slider-control.next', function (event) {
                    sliderFun.toggleSlide('next');
                });
            },
            // slider
            sliderInit: function () {
                sliderFun.controlInit();
                // 模式选择
                if (settings.sliderMode == 'slide') {
                    // slide 模式
                    $sliderWrap.width(sliderWidth * sliderCount);
                    $sliderWrap.children().width(sliderWidth);
                } else {
                    // mode 模式
                    $sliderWrap.children().css({
                        'position': 'absolute',
                        'left': 0,
                        'top': 0
                    });
                    $sliderWrap.children().first().siblings().hide();
                }
                // 控制事件
                if (settings.pointerEvent == 'hover') {
                    $slider.find('.slider-pointer > li').mouseenter(function (event) {
                        sliderFun.sliderPlay($(this).index());
                    });
                } else {
                    $slider.find('.slider-pointer > li').click(function (event) {
                        if (currentIndex != $(this).index()) {
                            sliderFun.sliderPlay($(this).index())
                        }
                    });
                }
                // 自动播放
                sliderFun.autoPlay();
            },
            // slidePlay
            sliderPlay: function (index) {
                sliderFun.stop();
                isAnimating = true;
                $sliderWrap.children().first().stop(true, true);
                $sliderWrap.children().stop(true, true);
                $slider.find('.slider-pointer').children()
                    .eq(index).addClass('active')
                    .siblings().removeClass('active');
                if (settings.sliderMode == "slide") {
                    // slide
                    if (index > currentIndex) {
                        $sliderWrap.animate({
                            left: '-=' + Math.abs(index - currentIndex) * sliderWidth + 'px'
                        }, settings.animateSpeed, function () {
                            sliderFun.stop();
                            isAnimating = false;
                            sliderFun.autoPlay()
                        });
                    } else if (index < currentIndex) {
                        $sliderWrap.animate({
                            left: '+=' + Math.abs(index - currentIndex) * sliderWidth + 'px'
                        }, settings.animateSpeed, function () {
                            isAnimating = false;
                            sliderFun.autoPlay();
                        });
                    } else {
                        return;
                    }
                } else {
                    // fade
                    if ($sliderWrap.children(':visible').index() == index) return;
                    $sliderWrap.children().fadeOut(settings.animateSpeed)
                        .eq(index).fadeIn(settings.animateSpeed, function () {
                        isAnimating = false;
                        sliderFun.autoPlay();
                    });
                }
                currentIndex = index;
            },
            // toggleSlide
            toggleSlide: function (arrow) {
                if (isAnimating) {
                    return;
                }
                var index;
                if (arrow == 'prev') {
                    index = (currentIndex == 0) ? sliderCount - 1 : currentIndex - 1;
                    sliderFun.sliderPlay(index);
                } else if (arrow == 'next') {
                    index = (currentIndex == sliderCount - 1) ? 0 : currentIndex + 1;
                    sliderFun.sliderPlay(index);
                }
            },
            // autoPlay
            autoPlay: function () {
                if (settings.isAuto) {
                    interval = setInterval(function () {
                        var index = currentIndex;
                        (currentIndex == sliderCount - 1) ? index = 0 : index = currentIndex + 1;
                        sliderFun.sliderPlay(index);
                    }, settings.transTime);
                } else {
                    return;
                }
            },
            //stop
            stop: function () {
                clearInterval(interval);
            },
        };
        sliderFun.sliderInit();
    }
    $(".sousuo-img").click(function(){
        $(".select").css("display","block");
        $(this).addClass("sousuo-select")
    })
    $(".all").click(function(){
        $(".sousuo-img").removeClass("sousuo-select");
    })
    $(".lookmore.newstb").click(function(){
        window.location.href="newslb.html"
    })
    //开启搜索
    $(".search-icon").click(function(){
        $("#jMenu").css("-webkit-animation","myfirst 1s");
        $(".search-wrapper").css("-webkit-animation","myfirst 1s");
        $(".search-Div").css("-webkit-animation","myfirst2 1.5s");
        $(".hot-search").css("display","block")
        $(".sousuo-input").focus();
        //把存储的数据显示出来作为搜索历史
        MapSearchArr();
        $.ajax({
            url:"front/search/show.do",
            type:"POST",
            success:function (xml) {
                $(xml.message).each(function(i,o){
                    var rmss = $("<div class='hot-nr'>"+o+"</div>").appendTo(".rmss")
                })
            }
        })
        event.stopPropagation();
    })
    //关闭搜索
    $(".search-Div-close").click(function(event){
        event.stopPropagation();
        $("#jMenu").css("-webkit-animation","myfirst3 1.5s");
        $(".search-wrapper").css("-webkit-animation","myfirst3 1.5s");
        $(".search-Div").css("-webkit-animation","myfirst4 1s");
        $(".hot-search").css("display","none")
        $(".rmss").children().remove();
    })
    //阻止点击输入框关闭搜索
    $(".sousuo-input").click(function () {
        event.stopPropagation();
    })
    //阻止点击最近搜索关闭搜索
    $(".hot-search").click(function () {
        event.stopPropagation();
    })

    //最近搜索
    $(".llss").on("click",".nrseach",function () {
        $(".sousuo-input").val($(this).text());
        $(".search-Div-search").click();
    })
    //删除历史记录
    $(".scjl").click(function () {
        //把localStorage记录删除
        localStorage.removeItem('search');
        searchArr=[];
        //把存储的数据显示出来作为搜索历史
        MapSearchArr();
    })
    //热门搜索
    $(".rmss").on("click",".hot-nr",function () {
        $(".sousuo-input").val($(this).text());
        $(".search-Div-search").click();
    })
    //点击其他地方关闭搜索
    $("body").click(function(event){
        if ($("#jMenu").css("opacity")=="1"){

        }else if ($("#jMenu").css("opacity")=="0")
        {
            event.stopPropagation();
            $("#jMenu").css("-webkit-animation","myfirst3 1s");
            $(".search-wrapper").css("-webkit-animation","myfirst3 1s");
            $(".search-Div").css("-webkit-animation","myfirst4 1s");
            $(".hot-search").css("display","none")
            $(".rmss").children().remove();
        }
    })
    $("#jMenu").jMenu();
    $("#logo").click(function () {
        window.location.href="qzsmk.html";
    })
    //搜索功能
    $(".search-Div-search").click(function () {
        event.stopPropagation();
        var val = $(".sousuo-input").val();
        //点击搜索按钮时，去重
        KillRepeat(val);
        //去重后把数组存储到浏览器localStorage
        localStorage.search = searchArr;
        //然后再把搜索内容显示出来
        MapSearchArr();
        localStorage.ss = val;
        window.location.href="sousuo.html";
        $(".sousuo-input").val("");
    });

    var searchArr;
//定义一个search的，判断浏览器有无数据存储（搜索历史）
    if(localStorage.search){
//如果有，转换成 数组的形式存放到searchArr的数组里（localStorage以字符串的形式存储，所以要把它转换成数组的形式）
        searchArr= localStorage.search.split(",")
    }else{
//如果没有，则定义searchArr为一个空的数组
        searchArr = [];
    }

    //去重
    function KillRepeat(val){
        var kill = 0;
        for (var i=0;i<searchArr.length;i++){
            if(val===searchArr[i]){
                kill ++;
            }
        }
        if(kill<1){
            if (searchArr.length>2){
                searchArr.shift();
                searchArr.push(val);
            }else {
                searchArr.push(val);
            }
        }
    }
    //历史记录生成
    function MapSearchArr(){
        var tmpHtml = "";
        var  searchArr2 = searchArr.reverse()
        for (var i=0;i<searchArr.length;i++){
            tmpHtml += "<div class=\"nrseach\">" + searchArr[i] + "</div>"
        }
        $(".llss").html(tmpHtml);
    }



})(jQuery);
jQuery(document).ready(function ($) {
    $('#slider').Slider();
    $(".exit").click(function () {
        window.location.href="login.html";
    })
});

